//
//  ViewController.m
//  HealthCA_H5
//
//  Created by 创维海通 on 2017/7/5.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate,JSObjcDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) JSContext *jsContext;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initWebView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -init
- (void)initWebView
{
//    NSURL *url = [NSURL URLWithString:@"http://192.168.1.42:8085/static/hcyx/index.html#/"];
//    
//    self.webView.delegate = self;
////    self.webView.mediaPlaybackAllowsAirPlay = YES;
//    self.webView.allowsInlineMediaPlayback = YES;
////    self.webView.suppressesIncrementalRendering = YES;
//    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"healthca/hcyx/index.html" relativeToURL:[[NSBundle mainBundle] bundleURL]]]];
}

#pragma mark -webdelegate
/**
 <#Description#>

 @param webView <#webView description#>
 @param request <#request description#>
 @param navigationType <#navigationType description#>
 @return <#return value description#>
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    return true;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"tianbai"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

- (void)getCall:(NSString *)callString{
    NSLog(@"Get:%@", callString);

    NSString *temp = [callString stringByReplacingOccurrencesOfString:@"data:image/png;base64," withString:@""];
    NSData *imageData = [[NSData alloc] initWithBase64EncodedString:temp options:NSDataBase64DecodingIgnoreUnknownCharacters];
    UIImage *image = [UIImage imageWithData:imageData];
    [self saveImageToPhotos:image];
}

- (void)saveImageToPhotos:(UIImage*)savedImage
{
     UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

@end
